package com.softeem.music.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.softeem.music.comm.R;
import com.softeem.music.entity.Admin;
import com.softeem.music.entity.User;
import com.softeem.music.service.UserService;
import com.softeem.music.mapper.UserMapper;
import org.springframework.stereotype.Service;

/**
* @author w3273
* @description 针对表【user】的数据库操作Service实现
* @createDate 2025-10-28 11:42:52
*/
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
    implements UserService{
    @Override
    public R login(String username, String password) {
        //根据用户名査询用户
        QueryWrapper<User> query = Wrappers.query(User.class);
        query.eq("username",username);
        //根据查询条件查询用户
        User user = getOne(query);
        if(user==null){
            return R.error().setMsg("用户名不存在");
        }
        //账号是否禁用
        if(user.getStatus()==0){
            return R.error().setMsg("账号被禁用");
        }
        //检查密码是否错误
        if(!user.getPassword().equals(password)){
            return R.error().setMsg("密码错误");
        }
        return R.success().setData(user);
    }


    /**
     * 注册
     * @param user
     * @return
     */
    @Override
    public R register(User user) {
        //检查用户名是否已存在
        QueryWrapper<User> query = Wrappers.query(User.class);
        query.eq("username",user.getUsername());
        //根据查询条件查询用户
        if (exists(query)){
            return R.error().setMsg("用户名已存在");
        }
        //保存用户
        if (save(user)){
            return R.success().setData(user);
        }
        //注册失败
        return R.error().setMsg("注册失败");
    }

}




